"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".

Replace these with more appropriate tests for your application.
"""

from django.test import TestCase
from pyceab.student.models import Student, DegreeRequirements
from pyceab.plan.models import Plan, PlannedCourse
from pyceab.course.models import Course

class TestCeabValidation(TestCase):
    fixtures = ['valid.json']

    def setUp(self):
        self.student = Student.objects.get(pk=1)
        self.p = Plan()
        self.p.student = self.student
        self.p.save()

    def test_min_course_load_validation(self):
        for i in xrange(1,9):
            c = Course.objects.get(pk=999)
            term_course = PlannedCourse(plan=self.p, course=c, term=i)
            term_course.save()

        self.assertEquals({}, self.p.check_course_load())

    def test_invalid_course_load(self):
        for i in xrange(1,9):
            c = Course.objects.get(pk=999)
            term_course = PlannedCourse(plan=self.p, course=c, term=i)
            term_course.save()

        self.student.degree_requirement = DegreeRequirements.objects.get(pk=2)
        self.student.save()

        errors = self.p.check_course_load()
        self.assertTrue('too_few_courses' in errors)
        self.assertEquals(4, errors['too_few_courses'])

    def test_simple_te_validation(self):
        for i in xrange(1,9):
            c = Course.objects.get(pk=999)
            term_course = PlannedCourse(plan=self.p, course=c, term=i)
            term_course.save()

        errors = self.p.check_te_hours()
        self.assertFalse(errors)

    def test_simple_cse_validation(self):
        for i in xrange(1,9):
            c = Course.objects.get(pk=999)
            term_course = PlannedCourse(plan=self.p, course=c, term=i)
            term_course.save()

        errors = self.p.check_cse_hours()
        self.assertFalse(errors)

    def test_complex_te_validation(self):
        for i in xrange(1,9):
            c = Course.objects.get(pk=999)
            term_course = PlannedCourse(plan=self.p, course=c, term=i)
            term_course.save()

        self.student.degree_requirement = DegreeRequirements.objects.get(pk=3)
        self.student.save()

        errors = self.p.check_cse_hours()
        self.assertFalse(errors)

    def test_invalid_te_selection(self):
        for i in xrange(1,9):
           PlannedCourse(
                plan = self.p,
                course = Course.objects.get(pk=1003),
                term = i
           ).save()

           PlannedCourse(
                plan = self.p,
                course = Course.objects.get(pk=1009),
                term = i
           ).save()

        errors = self.p.validate_plan()
        self.assertTrue(errors)
        self.assertEquals(2, errors['engsci_too_low'])
        self.assertEquals(8, errors['list_a_too_low'])

